package com.example.demo.controller;

import com.example.demo.common.R;
import com.example.demo.common.RPage;
import com.example.demo.decorator.Authorize;
import com.example.demo.entity.allTitles.*;
import com.example.demo.service.AllTitlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/all-titles")
public class AllTitlesController {
    @Autowired
    private AllTitlesService allTitlesService;

    @GetMapping
    @Authorize(
            permission = "main::allTitles:selList",
            label = "分页查询标题"
    )
    public R<RPage<AllTitles>> selAllTitles(@ModelAttribute AllTitlesSelList allTitlesSelList) {
        return allTitlesService.selAllTitles(allTitlesSelList);
    }

    @GetMapping("/all")
    @Authorize(
            permission = "main::allTitles:selAll",
            label = "查询所有标题"
    )
    public R<List<AllTitles>> selAllAllTitles(@ModelAttribute AllTitlesSelAll allTitlesSelAll) {
        return allTitlesService.selAllAllTitles(allTitlesSelAll);
    }

    @GetMapping("/ids")
    @Authorize(
            permission = "main::allTitles:selOnes",
            label = "查询多个标题（根据id）"
    )
    public R<List<AllTitles>> selOnesAllTitles(@RequestParam Map<String, String> ids) {
        return allTitlesService.selOnesAllTitles(ids.values().stream().map(Integer::valueOf).toList().toArray(new Integer[0]));
    }

    @GetMapping("/{id}")
    @Authorize(
            permission = "main::allTitles:selOne",
            label = "查询单个标题"
    )
    public R<AllTitles> selOneAllTitles(@PathVariable Integer id) {
        return allTitlesService.selOneAllTitles(id);
    }

    @PostMapping
    @Authorize(
            permission = "main::allTitles:ins",
            label = "新增标题"
    )
    public R<Boolean> insAllTitles(@RequestBody AllTitlesInsOne allTitlesInsOne) {
        return allTitlesService.insAllTitles(allTitlesInsOne);
    }

    @PostMapping("/s")
    @Authorize(
            permission = "main::allTitles:inss",
            label = "批量新增标题"
    )
    public R<Boolean> insAllTitless(@RequestBody AllTitlesInsOne[] allTitlesInsOnes) {
        return allTitlesService.insAllTitless(allTitlesInsOnes);
    }

    @PutMapping
    @Authorize(
            permission = "main::allTitles:upd",
            label = "修改标题"
    )
    public R<Boolean> updAllTitles(@RequestBody AllTitlesUpdOne allTitlesUpdOne) {
        return allTitlesService.updAllTitles(allTitlesUpdOne);
    }

    @PutMapping("/s")
    @Authorize(
            permission = "main::allTitles:upds",
            label = "批量修改标题"
    )
    public R<Boolean> updAllTitless(@RequestBody AllTitlesUpdOne[] allTitlesUpdOnes) {
        return allTitlesService.updAllTitless(allTitlesUpdOnes);
    }

    @DeleteMapping
    @Authorize(
            permission = "main::allTitles:del",
            label = "删除标题"
    )
    public R<Boolean> delAllTitles(@RequestBody Integer[] ids) {
        return allTitlesService.delAllTitles(ids);
    }
}
